package drinkkiosk.cards;

import drinkkiosk.CardPanel;
import serialtool.SerialTool;
import drinkkiosk.DrinkKiosk;
import drinkkiosk.drinks.DisplaySpecialPanel;
import drinkkiosk.drinks.DrinkDatabase;
import drinkkiosk.drinks.SpecialDrink;
import java.util.LinkedList;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

/**SpecialsCard displays a Special Drink based on the drinks relevance to the
 * ambient temperature.
 * @author Charles Ritchea
 */
public class SpecialsCard extends JPanel {

    /**
     * 
     * @return
     */
    public LinkedList<SpecialDrink> getSpecialsList() {
		return specialsList;
	}

	private String tempReading = "";
	int tempReadingInt = 12;
	private int index;
	private DrinkDatabase drinkDatabase;
	private LinkedList<SpecialDrink> specialsList;
	/** The current SpecialDrink on display */
	private SpecialDrink special;

	/** Creates new form Card */
	public SpecialsCard() {
            try{
		SerialTool serialTool = new SerialTool();
		tempReadingInt = Integer.parseInt((serialTool.getResult("t")));
                                }
                catch(java.lang.Throwable t) {
                    
                }
System.out.println("read val " + tempReadingInt);

		initComponents();
		index = 0;
		drinkDatabase = new DrinkDatabase();
		drinkDatabase.loadSpecialsList();
		specialsList = drinkDatabase.getSpecialsList();
		if (!specialsList.isEmpty()) {
			special = drinkDatabase.getSpecialsList().getFirst();
			displaySpecialPanel1.setComponents(special);
		}
	}

        /**
         * 
         * @return
         */
        public DisplaySpecialPanel getDisplaySpecialPanel1() {
		return displaySpecialPanel1;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane = new javax.swing.JLayeredPane();
        nextSpecialButton = new drinkkiosk.buttons.NextSpecialButton();
        previousSpecialsButton = new drinkkiosk.buttons.PreviousSpecialsButton();
        welcomeButton = new drinkkiosk.buttons.WelcomeButton();
        sendButton = new drinkkiosk.buttons.SendButton();
        displaySpecialPanel1 = new drinkkiosk.drinks.DisplaySpecialPanel();
        backgroundImage = new javax.swing.JLabel();

        nextSpecialButton.setFont(new java.awt.Font("Tahoma", 0, 24));
        nextSpecialButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextSpecialButtonActionPerformed(evt);
            }
        });
        nextSpecialButton.setBounds(680, 490, 110, 50);
        jLayeredPane.add(nextSpecialButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        previousSpecialsButton.setFont(new java.awt.Font("Tahoma", 0, 24));
        previousSpecialsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousSpecialsButtonActionPerformed(evt);
            }
        });
        previousSpecialsButton.setBounds(10, 490, 160, 50);
        jLayeredPane.add(previousSpecialsButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        welcomeButton.setBounds(230, 490, 143, 50);
        jLayeredPane.add(welcomeButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        sendButton.setBounds(430, 490, 209, 51);
        jLayeredPane.add(sendButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        displaySpecialPanel1.setBounds(80, 40, 641, 281);
        jLayeredPane.add(displaySpecialPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        backgroundImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/drinkkiosk/Welcome.png"))); // NOI18N
        backgroundImage.setBounds(0, 0, 800, 560);
        jLayeredPane.add(backgroundImage, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 555, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void nextSpecialButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextSpecialButtonActionPerformed

		if (index < specialsList.size() - 1) {
			index++;
		} else {
			index = 0;
		}
		if (tempReadingInt <= 60) {
			while ((specialsList.get(index).getTemperature().equalsIgnoreCase("iced")) || (specialsList.get(index).getTemperature().equalsIgnoreCase("frozen"))) {
				if (index < specialsList.size() - 1) {
					index++;
				} else {
					index = 0;
				}
			}
		}

		if (tempReadingInt > 80) {
			while ((specialsList.get(index).getTemperature().equalsIgnoreCase("hot"))) {
				if (index < specialsList.size() - 1) {
					index++;
				} else {
					index = 0;
				}
			}
		}
		special = specialsList.get(index);
		displaySpecialPanel1.setComponents(special);

	}//GEN-LAST:event_nextSpecialButtonActionPerformed

	private void previousSpecialsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousSpecialsButtonActionPerformed
		if (index > 0) {
			index--;
		} else {
			index = specialsList.size() - 1;
		}
		if (tempReadingInt <= 60) {
			while ((specialsList.get(index).getTemperature().equalsIgnoreCase("iced")) || (specialsList.get(index).getTemperature().equalsIgnoreCase("frozen"))) {
				if (index > 0) {
					index--;
				} else {
					index = specialsList.size() - 1;
				}
			}
		}

		if (tempReadingInt > 80) {
			while ((specialsList.get(index).getTemperature().equalsIgnoreCase("hot"))) {
				if (index > 0) {
					index--;
				} else {
					index = specialsList.size() - 1;
				}
			}
		}
		special = drinkDatabase.getSpecialsList().get(index);
		displaySpecialPanel1.setComponents(special);
	}//GEN-LAST:event_previousSpecialsButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel backgroundImage;
    private drinkkiosk.drinks.DisplaySpecialPanel displaySpecialPanel1;
    private javax.swing.JLayeredPane jLayeredPane;
    private drinkkiosk.buttons.NextSpecialButton nextSpecialButton;
    private drinkkiosk.buttons.PreviousSpecialsButton previousSpecialsButton;
    private drinkkiosk.buttons.SendButton sendButton;
    private drinkkiosk.buttons.WelcomeButton welcomeButton;
    // End of variables declaration//GEN-END:variables

	/** ShowDrink() displays the current Special in the SpecialsList on the
	 * SpecialsCard
	 */
	public void showDrink() {
	}

	/**
	 * 
	 * @return
	 */
	public SpecialDrink getSpecial() {
		return special;
	}

	/**
	 * 
	 * @param special
	 */
	public void setSpecial(SpecialDrink special) {
		this.special = special;
	}
}
